//
//  Proxy.swift
//  ProxyPattern
//
//  Created by LY on 2017/11/6.
//  Copyright © 2017年 DesignPatterns. All rights reserved.
//

import Foundation
protocol HttpHeaderRequest {
    func getHeader(url:String,header:String) -> String?
}
class HttpHeaderRequestProxy:HttpHeaderRequest {
    private let semaphore  = DispatchSemaphore(value: 0)
    func getHeader(url: String, header: String) -> String? {
        var headerValue : String?
        let Url = URL(string:url)
        let request = URLRequest(url: Url!)
        URLSession.shared.dataTask(with: request) { (data, responder, error) in
            if let httpResponse = responder as? HTTPURLResponse{
                headerValue = httpResponse.allHeaderFields[header] as? String
            }
            self.semaphore.signal()
        }.resume()
        semaphore.wait()
        return headerValue
    }
}
